﻿CREATE PROCEDURE dbo.procCreateSQLErrorLog
	@ErrorLogID int = 0 OUTPUT
AS

/****************************************************************************************************
 **        Name: procCreateSQLErrorLog
 ** Description: Creates a new SQLErrorLog record.
 ****************************************************************************************************
 ** Revisions
 **  Date        Author               Work Item  Description
 **  ----------  -------------------  ---------  ----------------------------------------------------
 **  11/30/2008  Chad Green             11774    Initial Development
 **  02/21/2009  Chad Green             11774    Rebuilt.
 ****************************************************************************************************/

SET NOCOUNT ON;

-- Initialize the output
SET @ErrorLogID = 0;

BEGIN TRY

    -- Return if there is no error information to log
    IF ERROR_NUMBER() IS NULL
        RETURN;

    -- Return if inside an uncommittable transaction. (Data insertion/modification is not allowed when a transaction is in an uncommittable state.)
    IF XACT_STATE() = -1
    BEGIN
        PRINT 'Cannot log error since the current transaction is in an uncommittable state. ' 
            + 'Rollback the transaction before executing uspLogError in order to successfully log error information.';
        RETURN;
    END

	-- Create the SQLErrorLog record
  INSERT dbo.SQLErrorLog (ErrorDateTime,
                          ErrorNumber,
                          ErrorSeverity,
                          ErrorState,
                          ErrorProcedure,
                          ErrorLine,
                          ErrorMessage,
                          Reviewed) 
                  VALUES (getDate(),
                          ERROR_NUMBER(),
                          ERROR_SEVERITY(),
                          ERROR_STATE(),
                          ERROR_PROCEDURE(),
                          ERROR_LINE(),
                          ERROR_MESSAGE(),
                          0);

  -- Pass back the ErrorLogID of the row inserted
  SET @ErrorLogID = @@IDENTITY;

END TRY
BEGIN CATCH
  PRINT 'An error occurred in stored procedure procLogError: ';
  EXECUTE dbo.procPrintError;
END CATCH
GO

EXEC sp_addextendedproperty @name='MS_Description', @value ='Creates a new SQLErrorLog record.', @level0type = 'SCHEMA', @level0name = 'dbo', @level1type = 'PROCEDURE', @level1name = 'procCreateSQLErrorLog', @level2type = NULL, @level2name = NULL
GO